package io.vertx.guides.wiki

import io.vertx.core.AbstractVerticle
import io.vertx.core.DeploymentOptions
import io.vertx.core.Future
import io.vertx.core.Vertx


/**
 * Created by ashye on 2017/7/18.
 */
class MainVerticle : AbstractVerticle() {

    companion object {

        @JvmStatic
        fun main(args: Array<String>) {
            val vertx = Vertx.factory.vertx()
            vertx.deployVerticle(MainVerticle())
        }
    }

    override fun start(startFuture: Future<Void>) {
        val dbVerticleDeployment = Future.future<String>()
        vertx.deployVerticle(WikiDatabaseVerticle(), dbVerticleDeployment.completer())

        dbVerticleDeployment.compose {
            val httpVerticleDeployment = Future.future<String>()
            vertx.deployVerticle(HttpServerVerticle::class.java.canonicalName,
                    DeploymentOptions().setInstances(2),
                    httpVerticleDeployment.completer())

            return@compose httpVerticleDeployment
        }.setHandler { ar ->
            if (ar.succeeded()) {
                startFuture.complete()
            }else {
                startFuture.fail(ar.cause())
            }
        }
    }
}

